<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

define('IN_ADMIN_COMPONENT_CONTROLLER', 'true');

require_once('includes/classes/Upload.php');

class ManufacturersAdministrativeComponent
{
	function ManufacturersAdministrativeComponent()
	{
	}

	function action(&$params, $action)
	{
		if ($action == 'Update') 
			return $this->edit($params);
		else
			return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);
		$languageId = (int)$params['languageId'];

		$value{$handle.'MetaData'}{'manufacturers_name_len_max'}= (int)MAX_DISPLAY_MANUFACTURER_NAME_LEN;

		$sqlQuery = 'select m.manufacturers_id, m.manufacturers_name, ' .
		'coalesce(m.manufacturers_image, "") as image, coalesce(mi.manufacturers_url, "") as url ' .
		'from ' . TABLE_MANUFACTURERS . ' m ' .
		'left join ' . TABLE_MANUFACTURERS_INFO . ' mi on (m.manufacturers_id = mi.manufacturers_id and mi.languages_id=' . $languageId . ') ' .
		'order by m.manufacturers_name';
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());

		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$value{$handle}{$i}{'id'}= (int)$row['manufacturers_id'];
			$value{$handle}{$i}{'name'}= $row['manufacturers_name'];
			$value{$handle}{$i}{'image'}= $row['image'];
			$value{$handle}{$i}{'url'}= $row['url'];
			$i++;
		}
		return $value;
	}

	function edit(&$params) 
	{
		$languageId = (int)$params['languageId'];
		$i = 0;
		while (true)
		{
			$i++;
			if ($i > 1000) break;

			$manufacturerId = (int)$params['manufacturerId-'.$i];
			if ($manufacturerId == 0) break;

			$manufacturerName = prepare_input($params['manufacturerName-'.$i]);
			if ($manufacturerName == null || strlen($manufacturerName) == 0) continue;

			$manufacturerImage = prepare_input($params['manufacturerImage-'.$i]);

			$manufacturerUrl = prepare_input($params['manufacturerUrl-'.$i]);

			$deleteRow = (boolean)$params['manufacturerDelete-'.$i];

			if ($deleteRow) {
				if ($manufacturerId < 1) continue;
				$sql = 'delete from ' . TABLE_MANUFACTURERS . ' where manufacturers_id=' . $manufacturerId;
				mysql_query($sql) or die(mysql_error());
				$sql = 'delete from ' . TABLE_MANUFACTURERS_INFO . ' where manufacturers_id=' . $manufacturerId;
				mysql_query($sql) or die(mysql_error());
			}
			else {
				$sqlAction = 'insert';
				$where = '';
				if ($manufacturerId > 0) {
					$sqlAction = 'update';
					$where = 'manufacturers_id=' . $manufacturerId;
				}

				$sql_data_array = array();

        			$image = new Upload($manufacturerImage, DIR_FS_CATALOG_IMAGES . 'manufacturers/', false);
				if ($image->parse() && $image->save()) {
					$sql_data_array['manufacturers_image'] = $image->filename;
				}
				else {
					if ($image->error_code < 0) {
						$this->errorCode = $image->error_code;
						$this->message[] = $image->message;
					}
				}

				$sql_data_array['manufacturers_name'] = $manufacturerName;
				db_perform(TABLE_MANUFACTURERS, $sql_data_array, $sqlAction, $where);

				$sql_data_array = array();
				if ($sqlAction == 'insert') {
					$manufacturerId = mysql_insert_id();
					$sql_data_array['manufacturers_id'] = $manufacturerId;
					$sql_data_array['languages_id'] = $languageId;
				}
				else {
					$where .= ' and languages_id=' . $languageId;
				}
				$sql_data_array['manufacturers_url'] = $manufacturerName;
				db_perform(TABLE_MANUFACTURERS_INFO, $sql_data_array, $sqlAction, $where);
			}
		}

		return $this->query($params);
	}
}
?>
